#!/bin/sh
##
## Start/stop script for Eddie ... Chris Miles <cmiles@connect.com.au> 980406
##
## $Id$
##

# Set this to receive Eddie restart/exception notifications from eddie_wrapper
#EDDIE_ADMIN=me@mydomain.com
#export EDDIE_ADMIN

EDDIEBASE=/opt/eddie
EDDIERUN=./eddie_wrapper

case "$1" in
'start')
	echo "Starting Eddie... \c"
	if [ ! -f /opt/eddie/config/eddie.cf ]; then
		echo "Error: there is no Eddie config."
		exit 1
	fi
	pid=`/usr/bin/ps -ef | /usr/bin/grep \.\*python\.\*eddie.py  | grep -v grep | awk '{ print $2 }'`
	if [ "${pid}" != "" ]; then
		echo "Error: Eddie is already running."
		exit 2
	fi
	if [ -x $EDDIEBASE ]; then
		cd $EDDIEBASE/bin
		if [ -x $EDDIERUN ]; then
			$EDDIERUN &
			echo done.
		else
			echo "Error: $EDDIERUN not found in $EDDIEBASE/bin"
		fi
	else
		echo "Error: $EDDIEBASE doesn't exist."
	fi
        ;;

'stop')
	echo "Stopping Eddie... \c"
	pid=`/usr/bin/ps -ef | /usr/bin/grep \.\*python\.\*eddie.py  | grep -v grep | awk '{ print $2 }'`
	if [ "${pid}" != "" ]; then
		/usr/bin/kill ${pid}
		echo done.
	else
		echo "Eddie was not running."
	fi     

	sleep 1

	echo "Stopping eddie_wrapper... \c"
	pid=`/usr/bin/ps -ef | /usr/bin/grep eddie_wrapper  | grep -v grep | awk '{ print $2 }'`
	if [ "${pid}" != "" ]; then
		/usr/bin/kill ${pid}
		echo done.
	else
		echo "eddie_wrapper was not running."
	fi     
        ;;

'reload')
	pid=`/usr/bin/ps -ef | /usr/bin/grep \.\*python\.\*eddie.py  | grep -v grep | awk '{ print $2 }'`
	echo "Reloading Eddie Config... \c"
	if [ "${pid}" != "" ]; then
		/usr/bin/kill -HUP ${pid}
		echo done.
	else
		echo "Eddie is not running."
	fi     
        ;;
*)
        echo "Usage: /etc/init.d/eddie { start | stop | reload }"
        ;;
esac

exit 0
